using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenTK.OpenGL;
using OpenTK.OpenGL.Enums;
using OpenTK.Math;

namespace vbo_test
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
      glControl1_Resize(this, EventArgs.Empty);
    }

    private void glControl1_Resize(object sender, EventArgs e)
    {
      if (glControl1.ClientSize.Height == 0)
        glControl1.ClientSize = new System.Drawing.Size(glControl1.ClientSize.Width, 1);

      GL.Viewport(0, 0, glControl1.ClientSize.Width, glControl1.ClientSize.Height);
      GL.MatrixMode(MatrixMode.Projection);
      GL.LoadIdentity();
      GL.Ortho(0, Width, 0, Height, 5, -5);
    }

    private void glControl1_Paint(object sender, PaintEventArgs e)
    {
      GL.Clear(ClearBufferMask.ColorBufferBit);
      glControl1.SwapBuffers();
    }

    private void glControl1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Escape)
        Close();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      int count = int.Parse(textBox1.Text);
      GL.Clear(ClearBufferMask.ColorBufferBit);
      MessageBox.Show("Drawing " + count.ToString() + " triangles using immediate mode");
      DateTime before = DateTime.Now;
      ImmediateDraw(count);
      TimeSpan ts = DateTime.Now - before;
      MessageBox.Show("Done. Took ms=" + ts.TotalMilliseconds.ToString());
      glControl1.SwapBuffers();
    }

    private void ImmediateDraw(int count)
    {
      GL.Begin(BeginMode.Triangles);
      Vector3 v1 = new Vector3(0, 0, 0);
      Vector3 v2 = new Vector3(Width, 0, 0);
      Vector3 v3 = new Vector3(0, Height, 0);
      for (int i = 0; i < count; i++)
      {
        GL.Vertex3(v1);
        GL.Vertex3(v2);
        GL.Vertex3(v3);
      }
      GL.End();
    }
  }
}
